package com.atguigu.mr1;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class FruitDriver implements Tool {
    //定义一个configuration
    private Configuration cfg=null;
    public int run(String[] args) throws Exception {
        //1 获取Job对象
        Job job = Job.getInstance(cfg);
        //2 设置驱动类路径
        job.setJarByClass(FruitDriver.class);
        //3 设置Mapper&Maper输出的KV类型
        job.setMapperClass(FruitMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(Text.class);
        //4 设置Reducer类
        TableMapReduceUtil.initTableReducerJob(args[1],FruitReducer.class,job);
        //5 设置输入参数
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        //6 提交任务
        boolean result = job.waitForCompletion(true);
        return result?0:1;
    }

    public void setConf(Configuration configuration) {
        cfg=configuration;
    }

    public Configuration getConf() {
        return cfg;
    }
    //执行代码
    //yarn jar hbase-demo-1.0-SNAPSHOT.jar com.atguigu.mr1.FruitDriver /fru.tsv fruit1
    public static void main(String[] args){
        try{
            Configuration configuration=new Configuration();
            int run = ToolRunner.run(configuration, new FruitDriver(), args);
            System.exit(run);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
